package per.turtle.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import per.turtle.common.core.constant.CommonConstants;
import per.turtle.system.entity.SysLoginInfo;
import per.turtle.system.mapper.SysLoginInfoMapper;
import per.turtle.system.service.ISysLoginInfoService;

import java.util.List;

/**
 * 系统访问日志情况信息 服务层实现
 *
 * @author turtle
 */
@Service
public class SysLoginInfoServiceImpl extends ServiceImpl<SysLoginInfoMapper, SysLoginInfo> implements ISysLoginInfoService {

    @Override
    public void cleanLoginInfo() {
        baseMapper.cleanLoginInfo();
    }

    @Override
    public int deleteLoginInfoByIds(List<Long> infoIds) {
        return baseMapper.deleteBatchIds(infoIds);
    }

    @Override
    public int insertLoginInfo(SysLoginInfo loginInfo) {
        return baseMapper.insert(loginInfo);
    }

    @Override
    public List<SysLoginInfo> selectLoginInfoList(SysLoginInfo loginInfo) {
        QueryWrapper<SysLoginInfo> wrapper = new QueryWrapper<>(loginInfo);
        wrapper.getEntity().setUserName(null);
        wrapper.getEntity().setIpaddr(null);
        wrapper.lambda().like(StrUtil.isNotBlank(loginInfo.getUserName()), SysLoginInfo::getUserName, loginInfo.getUserName())
               .like(StrUtil.isNotBlank(loginInfo.getIpaddr()), SysLoginInfo::getIpaddr, loginInfo.getIpaddr());
        wrapper.lambda().le(loginInfo.getParams().get(CommonConstants.END_TIME) != null, SysLoginInfo::getAccessTime,
                            loginInfo.getParams().get(CommonConstants.END_TIME));
        wrapper.lambda().ge(loginInfo.getParams().get(CommonConstants.START_TIME) != null, SysLoginInfo::getAccessTime,
                            loginInfo.getParams().get(CommonConstants.START_TIME));
        return baseMapper.selectList(wrapper);
    }

}
